package pro.shushi.pamirs.hr.core.action.eam;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import pro.shushi.pamirs.hr.api.enums.eam.MmdTypeEnum;
import pro.shushi.pamirs.hr.api.enums.eam.MtoolPjTypeEnum;
import pro.shushi.pamirs.hr.api.enums.eam.EamExpEnum;
import pro.shushi.pamirs.hr.api.model.eam.BsMtoolMd;
import pro.shushi.pamirs.hr.api.model.eam.EamMtoolMtHd;
import pro.shushi.pamirs.hr.api.model.eam.EamMtoolMtPj;
import pro.shushi.pamirs.meta.annotation.Action;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.annotation.Model;
import pro.shushi.pamirs.meta.annotation.fun.extern.Slf4j;
import pro.shushi.pamirs.meta.common.exception.PamirsException;
import pro.shushi.pamirs.meta.constant.ExpConstants;
import pro.shushi.pamirs.meta.constant.FunctionConstants;
import pro.shushi.pamirs.meta.enmu.FunctionTypeEnum;
import pro.shushi.pamirs.meta.enmu.ViewTypeEnum;

import java.util.List;

/**
 * 保养项自定义
 */
@Slf4j
@Component
@Model.model(EamMtoolMtHd.MODEL_MODEL)
public class EamMtoolMtHdAction {

    /**
     * 自定义构造方法示例
     * 如：界面设计器提交数据-选择的提交函数，新增时系统会调用construct
     * @param data
     * @return
     */
//    @Function(openLevel = FunctionOpenEnum.API)
//    @Function.Advanced(type = FunctionTypeEnum.QUERY)
//    public BusinessTrip construct(BusinessTrip data) {
//        data.setEmployName(HrSimpleSession.getEmployeeName());
//        data.setStartTime(new Date());
//        return data;
//    }
//
    @Action.Advanced(name = FunctionConstants.create, managed = true, invisible = ExpConstants.idValueExist)
    @Action(displayName = "保存", summary = "添加", bindingType = ViewTypeEnum.FORM)
    @Function(name = FunctionConstants.create)
    @Function.fun(FunctionConstants.create)
    public EamMtoolMtHd create(EamMtoolMtHd data) {

        // 预留扩展点，进行一些个性化的处理
//        Ext.run(BusinessTripServiceExtpoint::customHandler, data);
        //校验工装特殊分类与保养项
        this.checkMmdHdAndPj(data);

        data.construct();
        data.create();
        // 如果create重写了，关系型字段需要手动save
        data.fieldSave(EamMtoolMtHd::getEamMtoolMtPjs);
        return data;
    }

    // 重写默认的更新保存函数(如果默认满足则不需要重写)
    @Transactional
    @Function.Advanced(type = FunctionTypeEnum.UPDATE)
    @Action.Advanced(name = FunctionConstants.update, managed = true, invisible = ExpConstants.idValueNotExist)
    @Action(displayName = "更新", summary = "修改", bindingType = ViewTypeEnum.FORM)
    @Function(name = FunctionConstants.update)
    @Function.fun(FunctionConstants.update)
    public EamMtoolMtHd update(EamMtoolMtHd data) {

        //校验工装特殊分类与保养项
        this.checkMmdHdAndPj(data);

        data.updateById();
        // 如果create重写了，关系型字段需要手动save
        data.fieldSave(EamMtoolMtHd::getEamMtoolMtPjs);
        return data;
    }

    /**
     * 校验工装特殊分类与保养项
     */
    private void checkMmdHdAndPj(EamMtoolMtHd data){
        String orgCode = data.getOrgCode();
        String mmdCode = data.getMmdCode();
        BsMtoolMd mdRow = new BsMtoolMd().setOrgCode(orgCode).setMmdCode(mmdCode).queryOne();
        if (mdRow == null) {
            //工装型号：{}在组织编码：{}中未定义。
            throw PamirsException.construct(EamExpEnum.MTOOLMD_NOT_EXISTS,orgCode,mmdCode).errThrow();
        }
        int mmdType = mdRow.getMmdType().getValue();

        List<EamMtoolMtPj> pjList = data.getEamMtoolMtPjs();
        for(EamMtoolMtPj pjRow : pjList){
            /*保存前检验：若工装的特殊分类 not in(钢网) and 保养项目有保养项类型有张力测试 or
            工装的特殊分类 not in(刮刀) and 保养项目有保养项类型有平整度测试时，报错：工装型号编码：{0}
            的特殊分类为：{1}，保养项目不允许包含：{2}。 */
            boolean flag = false;
            if(mmdType != MmdTypeEnum.STENCIL.getValue()
                    && pjRow.getMtoolPjType().getValue() == MtoolPjTypeEnum.TENSION.getValue()){
                flag = true;
            } else if(mmdType != MmdTypeEnum.SCRAPER.getValue()
                && pjRow.getMtoolPjType().getValue() == MtoolPjTypeEnum.PLANENESS.getValue()){
                flag = true;
            }

            if(flag){
                //工装型号编码：{}的特殊分类为：{}，保养项目不允许包含：{}
                throw PamirsException.construct(EamExpEnum.PJ_TYPE_ERROR,mmdCode,mdRow.getMmdType().getDisplayName(),pjRow.getMtoolPjType().getDisplayName()).errThrow();
            }
        }




    }



}
